<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :destroy-on-close="true"
    title="支付通道"
    width="450px"
    @close="emits('closeDialog')"
  >
    <el-form
      ref="dialogFormRef"
      :model="dialogForm"
      :label-width="formLabelWidth"
      :rules="rules"
    >
      <el-form-item label="支付通道ID" prop="channel_id">
        <el-input v-model="dialogForm.channel_id" />
      </el-form-item>
      <el-form-item label="支付通道Code" prop="channel_code">
        <el-input v-model="dialogForm.channel_code" />
      </el-form-item>
      <el-form-item label="支付通道名称" prop="name">
        <el-input v-model="dialogForm.name" />
      </el-form-item>
      <el-form-item label="支付通道简介" prop="brief">
        <el-input v-model="dialogForm.brief" />
      </el-form-item>
      <el-form-item label="通道所属公司" prop="company">
        <el-input v-model="dialogForm.company" />
      </el-form-item>
      <el-form-item label="通道类型" prop="category">
        <el-input v-model="dialogForm.category" />
      </el-form-item>
      <el-form-item label="是否生效" prop="is_valid">
        <el-select v-model="dialogForm.is_valid" placeholder="是否生效">
          <el-option label="未生效" :value="0" />
          <el-option label="已生效" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否允许退款" prop="is_refund">
        <el-select v-model="dialogForm.is_refund" placeholder="是否允许退款">
          <el-option label="否" :value="0" />
          <el-option label="是" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="通道图标" :label-width="formLabelWidth">
        <el-upload
          :action="uploadimgUrl"
          :data="{ project_code: 'pay_service' }"
          :show-file-list="false"
          :on-success="handleUploaderImgSuccess"
          :limit="1"
          :headers="TOKENT_HEADER"
          class="avatar-uploader"
        >
          <img
            v-if="dialogForm.icon"
            :src="dialogForm.icon_show"
            class="avatar"
          />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
        <div class="flex-1" style="margin-left: 20px" v-if="dialogForm.icon">
          <el-icon style="color: red; cursor: pointer" @click="handleRemove">
            <Delete />
          </el-icon>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('cancel')">取消</el-button>
        <el-button
          type="primary"
          @click="submit(dialogForm)"
          :loading="btnLoading"
        >
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref } from "vue";
import { uploadimgUrl } from "@/api/system/uploadimg";
import { TOKENT_HEADER } from "@/config/api-constant/system";
const dialogFormRef = ref(null);

defineProps({
  dialogFormVisible: {
    type: Boolean,
    default: false,
  },
  dialogForm: {
    type: Object,
    default: {
      channel_id: "",
      channel_code: "",
      is_valid: "",
      is_refund: "",
      name: "",
      brief: "",
      company: "",
      category: "",
      icon: "",
      icon_show: "",
    },
  },
  btnLoading: {
    type: Boolean,
    default: false,
  },
});
const emits = defineEmits([
  "closeDialog",
  "cancel",
  "submitForm",
  "handleUploaderImgRequest",
]);
const handleUploaderImgSuccess = (res) => {
  emits("handleUploaderImgRequest", res);
};
const formLabelWidth = "130px";
// 校验
const rules = {
  name: [{ required: true, message: "请输入支付通道名称", trigger: "blur" }],
  brief: [{ required: true, message: "请输入支付通道简介", trigger: "blur" }],
  channel_id: [
    { required: true, message: "请输入支付通道ID", trigger: "blur" },
  ],
  channel_code: [
    { required: true, message: "请输入支付通道Code", trigger: "blur" },
  ],
};
// 提交
const submit = (dialogForm) => {
  dialogFormRef.value
    .validate()
    .then(async () => {
      try {
        const param = Object.assign({}, dialogForm);
        emits("submitForm", param);
      } catch (error) {
        return false;
      }
    })
    .catch((error) => {
      return false;
    });
};

const handleRemove = () => {
  emits("handleRemove");
};
</script>
